//
//  SplashADManager.m
//  HybidMediation
//
//  Created by jay Win on 2019/11/1.
//  Copyright © 2019 jay Win. All rights reserved.
//

#import "SplashADManager.h"

@interface SplashADManager()
@property NSString * app_id;
@property NSString * splash_id;
@end

@interface SplashADManager()<BUSplashAdDelegate>

@end

@implementation SplashADManager

+ (instancetype)shareInstance{
    static SplashADManager *manager=nil;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         manager = [SplashADManager new];
     });
     return manager;
}

- (void)loadSplash:(UIViewController *)rootViewController customView:(UIView *)logoView{
    if(![[[NSLocale currentLocale]objectForKey:NSLocaleIdentifier]hasSuffix:@"CN"]){
        [self.delegate onSplashLoadedFailed:@"!CN"];
            return;
    }
    if([KKADManager shareInstance].adsConfig==nil){
        NSLog(@"请先初始化广告");
        return;
    }
    if([[KKADManager shareInstance].adsConfig objectForKey:@"tt_app_id"]){
        _app_id=[[KKADManager shareInstance].adsConfig objectForKey:@"tt_app_id"];
    }else{
        NSLog(@"头条app_id为空，请在AdsConfig文件中配置app_id字段");
        return;
    }
    if([[KKADManager shareInstance].adsConfig objectForKey:@"tt_splash_id"]){
        _splash_id=[[KKADManager shareInstance].adsConfig objectForKey:@"tt_splash_id"];
    }else{
        NSLog(@"头条开屏id为空，请在AdsConfig文件中配置tt_splash_id字段");
        return;
    }
    [BUAdSDKManager setAppID:_app_id];
    if(self.debug){
      [BUAdSDKManager setLoglevel:BUAdSDKLogLevelDebug];
    }
    [BUAdSDKManager setIsPaidApp:NO];
    int splashHeight=0;
    if(logoView){
        splashHeight=SCREEN_HEIGHT-logoView.frame.size.height;
    }else{
        splashHeight=SCREEN_HEIGHT;
    }
    CGRect frame = [UIScreen mainScreen].bounds;
    BUSplashAdView * splash=[[BUSplashAdView alloc]initWithSlotID:_splash_id frame:frame];
    splash.delegate=self;
    splash.rootViewController=rootViewController;
    [rootViewController.view addSubview:splash];
    [splash loadAdData];
    [self.delegate onSplashRequest];
}

- (void)loadSplash:(UIViewController *)rootViewController customView:(UIView *)logoView onImpressionEvent:(NSString * _Nonnull (^)(void))onImpression onFailedEvent:(NSString * _Nonnull (^)(void))onFailed{
    self.onSplashFailed = onFailed;
    self.onSplashImpression = onImpression;
    
}

- (void)showlog:(NSObject *)obj{
    NSLog(@"===AD====:%@",obj);
}


//加载失败
- (void)splashAd:(BUSplashAdView *)splashAd didFailWithError:(NSError *)error{
    if(error){
         NSLog(@"=====开屏广告加载失败%@",error.localizedDescription);
        [self.delegate onSplashLoadedFailed:error.localizedDescription];
    }else{
        NSLog(@"=====开屏广告加载失败");
        [self.delegate onSplashLoadedFailed:@"未知错误"];
    }
    [splashAd removeFromSuperview];
}
//加载即将显示
- (void)splashAdWillVisible:(BUSplashAdView *)splashAd{
    [self showlog:@"开屏即将显示"];
    splashAd.alpha=1;
    if([[[KKADManager shareInstance]adsConfig]objectForKey:@"sp_im"]){
          [[KKAnalytics shareManager]logAdjustEvent:[[[KKADManager shareInstance]adsConfig]objectForKey:@"sp_im"]];
    }else{
        NSLog(@"===== ADJUST ERROR================请在AdsConfig中设置sp_im开屏打点Token");
    }
    [self.delegate onSplashImpression];
}
//加载成功
- (void)splashAdDidLoad:(BUSplashAdView *)splashAd{
    [self showlog:@"开屏加载成功"];
    [self.delegate onSplashLoaded];
}
//点击
- (void)splashAdDidClick:(BUSplashAdView *)splashAd{
    [self showlog:@"开屏点击"];
}
//即将关闭
- (void)splashAdWillClose:(BUSplashAdView *)splashAd{
    [self showlog:@"开屏即将关闭"];
}
//已经关闭
- (void)splashAdDidClose:(BUSplashAdView *)splashAd{
    [self showlog:@"开屏已经关闭"];
    [splashAd removeFromSuperview];
}



@end
